﻿using System.ComponentModel.Composition;
using System.Windows;
using MediaAssistant.DAL;
using MediaAssistant.Data;
using MediaAssistant.Management;
using MefBasic;
using MefBasic.Commans;
using Microsoft.Practices.Composite.Events;

namespace MediaAssistant.Controls.PlayerControl
{
    [Export]
    public class PlayerControlPresenter:APresenter<IPlayerControlView>
    {
        [ImportingConstructor]
        public PlayerControlPresenter(IPlayerControlView view, IEventAggregator eventAggregator) : base(view)
        {
            ChangeShuffleModeCommand = new DelegateCommand(ExecuteChangeShuffleMode);
            NextMusicCommand = new DelegateCommand(ExecuteNextMusic);
            PreviousMusicCommand = new DelegateCommand(ExecutePreviousMusic);
        }

        public DelegateCommand PreviousMusicCommand { get; set; }

        private void ExecutePreviousMusic(object obj)
        {
            DataSource.SelectedMusicDJ.Previous();
            RaiseCanExecuteChanged();
        }

        [Import]
        public LibraryManager LibraryManager
        {
            get { return (LibraryManager)GetValue(MyPropertyProperty); }
            set { SetValue(MyPropertyProperty, value); }
        }

        public static readonly DependencyProperty MyPropertyProperty =
            DependencyProperty.Register("MyProperty", typeof(LibraryManager), typeof(PlayerControlPresenter), new UIPropertyMetadata(null));

        
        private void RaiseCanExecuteChanged()
        {
            ChangeShuffleModeCommand.RaiseCanExecuteChanged();
            NextMusicCommand.RaiseCanExecuteChanged();
            PreviousMusicCommand.RaiseCanExecuteChanged();
        }

        public DelegateCommand NextMusicCommand { get; set; }

        private void ExecuteNextMusic(object obj)
        {
            DatabaseManager.Instance.AdvanceScore(DataSource.SelectedProfile, DataSource.PlayingMusic, -0.5);
            DataSource.SelectedMusicDJ.Next();
            RaiseCanExecuteChanged();
        }

        public DelegateCommand ChangeShuffleModeCommand { get; set; }

        private void ExecuteChangeShuffleMode(object obj)
        {
            DataSource.SelectMusicDJ(obj.ToString());
        }
       
        [Import]
        public LibraryDataSource DataSource
        {
            get { return (LibraryDataSource)GetValue(DataSourceProperty); }
            set { SetValue(DataSourceProperty, value); }
        }

        public static readonly DependencyProperty DataSourceProperty =
            DependencyProperty.Register("DataSource", typeof(LibraryDataSource), typeof(PlayerControlPresenter), new UIPropertyMetadata(null));
    }
}
